package com.ruoyi.project.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.domain.BomPartstRef;
import com.ruoyi.project.dto.ExcelimportPartstInfo;
import com.ruoyi.project.dto.exportPartstInfoTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.BomInfo;
import com.ruoyi.project.service.IBomInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * bom信息Controller
 * 
 * @author ruoyi
 * @date 2022-08-29
 */
@RestController
@RequestMapping("/project/bomInfo")
public class BomInfoController extends BaseController
{
    @Autowired
    private IBomInfoService bomInfoService;

    /**
     * 查询bom信息列表
     */
    @PreAuthorize("@ss.hasPermi('project:bomInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BomInfo bomInfo)
    {
        startPage();
        List<BomInfo> list = bomInfoService.selectBomInfoList(bomInfo);
        return getDataTable(list);
    }

    /**
     * 导出bom信息列表
     */
    @PreAuthorize("@ss.hasPermi('project:bomInfo:export')")
    @Log(title = "bom信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BomInfo bomInfo)
    {
        List<BomInfo> list = bomInfoService.selectBomInfoList(bomInfo);
        ExcelUtil<BomInfo> util = new ExcelUtil<BomInfo>(BomInfo.class);
        util.exportExcel(response, list, "bom信息数据");
    }

    /**
     * 获取bom信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:bomInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bomInfoService.selectBomInfoById(id));
    }

    /**
     * 新增bom信息
     */
    @PreAuthorize("@ss.hasPermi('project:bomInfo:add')")
    @Log(title = "bom信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BomInfo bomInfo)
    {
        if (StringUtils.isNull(bomInfo))
            throw new ServiceException("数据不能为空");
        return toAjax(bomInfoService.insertBomInfo(bomInfo));
    }

    /**
     * 修改bom信息
     */
    @PreAuthorize("@ss.hasPermi('project:bomInfo:edit')")
    @Log(title = "bom信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BomInfo bomInfo)
    {
        if (StringUtils.isNull(bomInfo))
            throw new ServiceException("bom不能为空");
        if (StringUtils.isNull(bomInfo.getId()))
            throw new ServiceException("bomId");
        return toAjax(bomInfoService.updateBomInfo(bomInfo));
    }

    /**
     * 删除bom信息
     */
    @PreAuthorize("@ss.hasPermi('project:bomInfo:remove')")
    @Log(title = "bom信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bomInfoService.deleteBomInfoByIds(ids));
    }

    /**
     * 获取构件的楼层信息及bom信息
     */
    //todo
    @Log(title = "bom信息", businessType = BusinessType.DELETE)
    @GetMapping("/getBulidFloor")
    @PreAuthorize("@ss.hasPermi('project:bomInfo:getBulidFloor')")
    public AjaxResult getBulidFloor(@RequestParam String compomentNo)
    {
        if (StringUtils.isNull(compomentNo))
            throw new ServiceException("bom不能为空");

        return bomInfoService.getBulidFloor(compomentNo);
    }

    /**
     * 绑定bom
     * @return
     */
    //todo
    @Log(title = "bom信息", businessType = BusinessType.DELETE)
    @PostMapping("/bindingBom")
    @PreAuthorize("@ss.hasPermi('project:bomInfo:bindingBom')")
    public AjaxResult bindingBom(@RequestBody JSONObject params)
    {
        String bomNo = params.getString("bomNo");
        List<Integer> floorId = params.getList("floorId", Integer.class);
        if (bomNo==null || bomNo.length()==0)
            throw new ServiceException("bom不能为空");

        if (floorId==null || floorId.size()==0)
            throw new ServiceException("楼层不能为空");

       return bomInfoService.bindingBom(floorId,bomNo);
    }

    /**
     * 解绑bom
     * @param
     * @return
     */
    //todo
    @Log(title = "bom信息", businessType = BusinessType.DELETE)
    @PostMapping("/unboundBom")
    @PreAuthorize("@ss.hasPermi('project:bomInfo:unboundBom')")
    public AjaxResult unboundBom(@RequestBody JSONObject params)
    {
        List<Integer> floorId = params.getList("floorId", Integer.class);
        if (floorId==null || floorId.size()==0)
            throw new ServiceException("楼层不能为空");
        return bomInfoService.bindingBom(floorId,null);
    }
    /**
     * 删除bom下的某个零件
     * @return
     */
    @Log(title = "bom信息", businessType = BusinessType.DELETE)
    @PostMapping("/delBompartst")
    @PreAuthorize("@ss.hasPermi('project:bomInfo:delBompartst')")
    public AjaxResult delBomPartst(@RequestBody JSONObject params)
    {
        Integer id = params.getInteger("id");
        if (id==null)
            throw new ServiceException("id不能为空");
        return bomInfoService.delBomPartst(id);
    }

    /**
     * 新增bom下的某个零件
     * @return
     */
    @Log(title = "bom信息", businessType = BusinessType.DELETE)
    @PostMapping("/addBompartst")
    @PreAuthorize("@ss.hasPermi('project:bomInfo:addBompartst')")
    public AjaxResult addBomPartst(@RequestBody JSONObject params)
    {
        List<BomPartstRef> bomPartstRefList=params.getList("bomPartstRefList",BomPartstRef.class);
        if (bomPartstRefList==null || bomPartstRefList.size()==0)
            throw new ServiceException("零件不能为空");
        return bomInfoService.addBomPartst(bomPartstRefList);
    }


/**
 * bom导入模板下载
 */
    @PreAuthorize("@ss.hasPermi('project:bomInfo:exportBomTemplate')")
    @Log(title = "bom信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportBomTemplate")
    public void exportBomTemplate(HttpServletResponse response)
    {
        ExcelUtil<ExcelimportPartstInfo> util = new ExcelUtil<ExcelimportPartstInfo>(ExcelimportPartstInfo.class);
        util.exportExcel(response, new ArrayList<>(), "bom信息数据");
    }

    /**
     * 零件导入模板下载
     */
    @PreAuthorize("@ss.hasPermi('project:bomInfo:exportPartstInfoTemplate')")
    @Log(title = "bom信息", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPartstInfoTemplate")
    public void exportPartstInfoTemplate(HttpServletResponse response)
    {
        ExcelUtil<exportPartstInfoTemplate> util = new ExcelUtil<exportPartstInfoTemplate>(exportPartstInfoTemplate.class);
        util.exportExcel(response, new ArrayList<>(), "零件");
    }
}
